package com.gx.smart.building.ui.report.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.github.vipulasri.timelineview.TimelineView;
import com.gx.smart.building.R;
import com.gx.smart.building.ui.repair.common.CommonHelper;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderEventDto;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaosy
 * @create 2020/5/12
 * @Describe
 **/
public class FaultDetailHandleAdapter extends RecyclerView.Adapter<FaultDetailHandleAdapter.ViewHolder> {

    private final List<WorkOrderEventDto> list;
    private final Context context;

    public FaultDetailHandleAdapter(Context context) {
        this.list = new ArrayList<>();
        this.context = context;
    }


    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position, getItemCount());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_timeline, parent, false);
        return new ViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WorkOrderEventDto item = list.get(position);
        holder.title.setText(item.getEventName());
        String creator = CommonHelper.getCreatorByEventCode(item.getEventCode());
        holder.subTitle.setText(creator + ": " + item.getHandleUserName());
        String time = TimeUtils.millis2String(item.getHandleTime(), "yyyy.MM.dd HH:mm");
        holder.time.setText(time);
        String eventCode = item.getEventCode();
        /**
         * 工单创建
         * DRAFT_TASK("draft_task", "工单创建"),
         * <p>
         * 工单指派
         * ASSIGN_TASK("assign_task", "工单指派"),
         * <p>
         * 工单接收
         * RECEIVE_TASK("receive_task", "工单接收"),
         * <p>
         * 处理完成
         * PROCESS_TASK("process_task", "处理完成"),
         * <p>
         * 工单回退
         * BACK_TASK("back_task", "工单回退"),
         * <p>
         * 工单转派
         * FORWARD_TASK("forward_task", "工单转派"),
         * <p>
         * 工单挂起
         * SUSPEND_TASK("suspend_task", "工单挂起"),
         * <p>
         * 工单解挂
         * RESUME_TASK("resume_task", "工单解挂"),
         * <p>
         * 工单审核
         * REVIEW_TASK("review_task", "工单审核"),
         * <p>
         * 挂起审批
         * SUSPEND_REVIEW_TASK("suspend_review_task", "挂起审批"),
         * <p>
         * 工单撤销
         * CANCEL_TASK("cancel_task", "工单撤销");
         */
        switch (eventCode) {
            case "assign_task":
                holder.twoSubTitle.setText("指派意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            case "receive_task":
                holder.twoSubTitle.setText("接单意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            case "back_task":
                holder.twoSubTitle.setText("回退意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            case "forward_task":
                holder.twoSubTitle.setText("转派意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            case "process_task":
                holder.twoSubTitle.setText("处理说明: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("处理用时: " + item.getHandleDurationDesc());
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.VISIBLE);
                break;
            case "suspend_review_task":
                //审核状态，1-通过，2-不通过
                holder.twoSubTitle.setText("审批结果: " + (item.getReviewStatus() == 1 ? "通过" : "不通过"));
                holder.threeSubTitle.setText("审批意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.VISIBLE);
                break;
            case "suspend_task":
                holder.twoSubTitle.setText("预计解挂时间: " + TimeUtils.millis2String(item.getHangEndTime(), "yyyy-MM-dd"));
                holder.threeSubTitle.setText("挂起意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.VISIBLE);
                break;
            case "resume_task":
                holder.twoSubTitle.setText("解挂意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            case "review_task":
                holder.twoSubTitle.setText("审核结果: " + (item.getReviewStatus() == 1 ? "通过" : "不通过"));
                holder.threeSubTitle.setText("审核意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.VISIBLE);
                break;
            case "cancel_task":
                holder.twoSubTitle.setText("撤消意见: " + (item.getOpinion() == null || item.getOpinion().isEmpty() ? "无" : item.getOpinion()));
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.VISIBLE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
            default:
                holder.twoSubTitle.setText("");
                holder.threeSubTitle.setText("");
                holder.twoSubTitle.setVisibility(View.GONE);
                holder.threeSubTitle.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void refresh(List<WorkOrderEventDto> newList) {
        this.list.clear();
        this.list.addAll(newList);
        notifyDataSetChanged();
    }


    public void clear() {
        this.list.clear();
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private final TimelineView timeline;
        private final AppCompatTextView title;
        private final AppCompatTextView time;
        private final AppCompatTextView subTitle;
        private final AppCompatTextView twoSubTitle;
        private final AppCompatTextView threeSubTitle;

        public ViewHolder(View view, int viewType) {
            super(view);
            timeline = (TimelineView) view.findViewById(R.id.timeline);
            timeline.initLine(viewType);

            title = (AppCompatTextView) view.findViewById(R.id.title);
            time = (AppCompatTextView) view.findViewById(R.id.time);
            subTitle = (AppCompatTextView) view.findViewById(R.id.subTitle);
            twoSubTitle = (AppCompatTextView) view.findViewById(R.id.twoSubTitle);
            threeSubTitle = (AppCompatTextView) view.findViewById(R.id.threeSubTitle);
        }
    }

}
